<?php
require_once APPLICATION_PATH.'/../library/Sozfo/Form.php'; 
class Application_Form_Edit_MailTemplate extends Sozfo_Form
{

    public $id = false;

    public function __construct($id) {
        $this->id = $id;
        parent::__construct();
    }

    public function init()
    {
        $this->setMethod('post');

        $languages = Application_Model_Language::getList();

        if (!empty($languages) && is_array($languages))
        {

            // Add input for key
            $code_validators = array(
                array('validator' => 'StringLength',      'options' => array(0, 128)),
            );
            $code_unique_validator = array('validator' => 'Db_NoRecordExists', 'options' => array('table' => 'mail_template', 'field' => 'code'));
            if ($this->id) {
                $code_unique_validator['options']['exclude'] = array(
                    'field' => 'id',
                    'value' => $this->id
                );
            }
            $code_validators[] = $code_unique_validator;
            $this->addElement('text', 'code', array(
                'label'      => 'Code',
                'required'   => true,
                'filters'    => array('StringTrim'),
                'validators' => $code_validators
            ));

            // Add input for email
            $this->addElement('text', 'from_email', array(
                'label'      => 'From email',
                'required'   => true,
                'filters'    => array('StringTrim'),
                'validators' => array(
                    array('validator' => 'StringLength', 'options' => array(0, 128)),
                    array('validator' => 'EmailAddress', 'options' => array('domain' => true))
                )
            ));

            // Add input for status
            $this->addElement('checkbox', 'allow_use', array(
                'label' => 'Enabled',
            ));

            foreach ($languages as $language)
            {
                // Add input for subject
                $this->addElement('text', 'subject_' . $language->getId(), array(
                    'label'      => 'Subject',
                    'required'   => true,
                    'filters'    => array('StringTrim'),
                    'validators' => array(
                    )
                ));

                // Add input for sender's name
                $this->addElement('text', 'from_name_' . $language->getId(), array(
                    'label'      => 'Sender name',
                    'required'   => true,
                    'filters'    => array('StringTrim'),
                    'validators' => array(
                    )
                ));

                // Add input for content
                $this->addElement('tinyMce', 'content_' . $language->getId(), array(
                    'label'         => 'Content',
                    'required'      => true,
                    'cols'          => '50',
                    'rows'          => '10',
                    'editorOptions' => new Zend_Config_Ini(APPLICATION_PATH . '/configs/tinymce.ini', 'moderator')
                ));
            }
        }

    }
}

